﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TDLib.DB
{
    public class MSSQLHelper2
    {
        private string m_dbconnstr;
        private int m_timeout = 0;

        public MSSQLHelper2(string connstr)
        {
            m_dbconnstr = connstr;
        }

        public MSSQLHelper2(string connstr,int timeout)
        {
            m_dbconnstr = connstr;
            m_timeout = timeout * 1000;
        }

        /// <summary>
        /// 从数据库中执行某一SQL命令，不返回数据集合(用指定的连接名)
        /// </summary>
        public int RunSQL(string strSQL)
        {
            int intResult = 0;
            
            SqlConnection conn = new SqlConnection(m_dbconnstr);
            conn.Open();

            SqlCommand cmd = new SqlCommand(strSQL, conn);
            if (m_timeout > 0)
                cmd.CommandTimeout = m_timeout;

            try
            {
                intResult = cmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }            

            return intResult;
        }

        /// <summary>
        /// 从数据库中执行某一SQL命令，不返回数据集合(用指定的连接名)
        /// </summary>
        public int RunSQL(SqlCommand cmd)
        {
            int intResult = 0;

            SqlConnection conn = new SqlConnection(m_dbconnstr);
            conn.Open();

            cmd.Connection = conn;
            if (m_timeout > 0)
                cmd.CommandTimeout = m_timeout;

            try
            {
                intResult = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }       

            conn.Close();

            return intResult;
        }

        /// <summary>
        /// 执行sql语句，并返回dataset
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns></returns>
        public DataSet RunSQLDS(string strSQL)
        {
            SqlConnection conn = new SqlConnection(m_dbconnstr);

            SqlCommand cmd = new SqlCommand(strSQL, conn);
            if (m_timeout > 0)
                cmd.CommandTimeout = m_timeout;
            SqlDataAdapter dad = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            dad.Fill(ds);

            return ds;

        }

        /// <summary>
        /// 执行sql语句，并返回dataset
        /// </summary>
        public DataSet RunSQLDS(SqlCommand cmd)
        {
            SqlConnection conn = new SqlConnection(m_dbconnstr);

            cmd.Connection = conn;
            if (m_timeout > 0)
                cmd.CommandTimeout = m_timeout;
            SqlDataAdapter dad = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            dad.Fill(ds);

            return ds;

        }
    }
}
